package com.wanmait.translate.configuration.other;

import com.wanmait.translate.system.service.impl.SystemUserServiceImpl;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MyRealm extends AuthorizingRealm {
    @Resource
    private SystemUserServiceImpl systemUserService;
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获得主体身份信息，即用户名
        String primaryPrincipal = (String)principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //在这里模拟数据库查询角色
        simpleAuthorizationInfo.addRole("admin");
        simpleAuthorizationInfo.addRole("user");
        return simpleAuthorizationInfo;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获得用户名
        String principal = (String) authenticationToken.getPrincipal();
        SimpleAuthenticationInfo simpleAuthorizationInfo=null;
        //在这里模拟数据库查询密码
        if (principal == "张三") {
            //三个参数时 第一个参数为用户名 第二个参数为密码 第三个为当前Realm的名字
            //四个参数时 第一个参数为用户名 第二个参数为MD5加随机盐后的密码 第三个为注册时随机盐的值       第四个为当前Realm的名字
            simpleAuthorizationInfo = new SimpleAuthenticationInfo(principal, "123", ByteSource.Util.bytes("assd"),"MyRealm");
        }
        return simpleAuthorizationInfo;
    }
}
